package org.devocean.groupbuying.web.frontend;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.devocean.groupbuying.dao.DealDAO;
import org.devocean.groupbuying.dao.ExploredDAO;
import org.devocean.groupbuying.domain.Customer;
import org.devocean.groupbuying.domain.Deal;
import org.devocean.groupbuying.domain.Explored;
import org.devocean.groupbuying.domain.LinkStat;
import org.devocean.groupbuying.domain.Media;
import org.devocean.groupbuying.domain.Plan;
import org.devocean.groupbuying.domain.Product;
import org.devocean.groupbuying.domain.Promotion;
import org.devocean.groupbuying.domain.Voucher;
import org.devocean.groupbuying.service.CustomerService;
import org.devocean.groupbuying.service.DealService;
import org.devocean.groupbuying.service.ExploredService;
import org.devocean.groupbuying.service.LinkStatService;
import org.devocean.groupbuying.service.PlanService;
import org.devocean.groupbuying.service.ProductService;
import org.devocean.groupbuying.service.PromotionService;
import org.devocean.groupbuying.service.PurchaseService;
import org.devocean.groupbuying.service.VoucherService;
import org.devocean.groupbuying.web.frontend.displaybeans.DealBean;
import org.devocean.groupbuying.web.frontend.displaybeans.PurchaseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = { "/redirect" })
public class RedirectController {

	@Autowired
	private LinkStatService linkStatService;

	@Autowired
	private CustomerService customerService;

	@RequestMapping("")
	public String index(ModelMap map, Principal principal,
			@CookieValue("clientLanguage") String lang,
			String url, HttpServletRequest request,
			 String from, Model model) {

		LinkStat ls = new LinkStat();
		ls.setLinkFrom(from);
		ls.setLinkDate(Calendar.getInstance().getTime());
		ls.setLinkTo(url);
		SecurityContext sc = (SecurityContext) request.getSession()
				.getAttribute("SPRING_SECURITY_CONTEXT");

		if (sc != null) {
			Customer customer = customerService.getCustomerById(Integer
					.valueOf(sc.getAuthentication().getName()));
			ls.setCustomer(customer);
		}

		linkStatService.addOrUpdateLinkStat(ls);

		model.asMap().clear();

		return "redirect:" + url;
	}
	

}
